package com.monkeyboy.manager.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisPoolFactory {
	@Autowired
	private RedisConfig redisConfig;
	@Bean
	public JedisPool jedisFactory() {
		JedisPoolConfig poolConfig=new JedisPoolConfig();
		//配置redis连接池的一些常用属性
		poolConfig.setMaxIdle(redisConfig.getPoolMaxldle());
		poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
		poolConfig.setMinIdle(redisConfig.getPoolMinIdle());
		poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait());
		poolConfig.setTestOnBorrow(redisConfig.isPoolTestOnBorrow());
		poolConfig.setTestOnReturn(redisConfig.isPoolTestOnReturn());
		poolConfig.setBlockWhenExhausted(redisConfig.isPoolBlockWhenExhausted());
		//初始化连接池，最后一个参数是选择使用redis的第几个数据库，一共16个
		JedisPool jedisPool=new JedisPool(poolConfig,redisConfig.getHost(),redisConfig.getPort(),
				redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
		return jedisPool;
	}
}
